Mapping the Java Objects
From Documentation
This documentation is for an older version of ZK. For the latest one, please click here.
There are two ways to tell Hibernate how to load and store objects of the persistent class, one is using Hibernate mapping file, and the other is using Java Annotation.
Using the Mapping Files
- Simply create
Event.hbm.xml
for the persistent classEvent.java
.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"[http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]">
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
- Place this
Event.hbm.xml
in the directory calledsrc
in the development folder, and its correct package. (ex.$myApp/WEB-INF/src/event/Event.hbm.xml
)
Using Java Annotation
The benefit of using Java annotation instead of Hibernate mapping file is that no additional file is required. Simply add Java annotation on your Java class to tell Hibernate about the mappings.
package events;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EVENTS")
public class Event {
private Long id;
private String title;
private Date date;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name = "EVENT_ID")
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
@Column(name = "EVENT_DATE")
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
- @Entity declares this class as a persistence object
- @Table(name = "EVENTS") annotation tells that the entity is mapped with the table EVENTS in the database
- @Column element is used to map the entities with the column in the database.
- @Id element defines the mapping from that property to the primary key column.